<?php
/**
 * 红包码
 */
namespace App;
defined('IN_IA') or exit('Access Denied');
class RedQrcode extends Action
{
    private $Red = null;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 前端canvas拼成红包码的数据
     * @param int red_id 红包id
     */
    public function canvasData()
    {
        //红包标题、昵称、头像、红包二维码、模板
        $_data = array();
        $_red = $this->getRedPackage($this->_gpc["red_id"]);
        $_data["title"] = $_red["title"];
        $_member = $this->uidToInfo($_red["uid"]);
        $_data["nickname"] = $_member["nickname"];
        if(KUYUAN_DEBUG){
            $_data["header"] = 'http://file.geekdm.com/header.jpg';
            $_data["tpl"] = 'http://file.geekdm.com/tpl1.jpg';
            $_data["qrcode"] = "http://file.geekdm.com/qrcode.png";
        }else{
            $_data["header"] = $this->wxHeaderToLocation($_member["avatar"],$_red["uid"]);;
            $_data["tpl"] = MODULE_URL."redtpl/default.jpg";
            $_url = murl('entry//index',array('m'=>'kuyuan_red','red_id'=>$this->_gpc['red_id'],'share_uid'=>$this->_gpc['uid']),true,true);
            $_data["qrcode"] = $this->getQrcode("qrcode","red_id_".$this->_gpc["red_id"],$_url);
        }
        unset($_member,$_url,$_red);
        $this->_result = array("status"=>"success","data"=>$_data);
        $this->result($this->_result);
    }

    /**
     * 将微信头像拉取到本地
     * @param $_url
     * @param $_uid
     * @return mixed
     */
    private function wxHeaderToLocation($_url,$_uid)
    {
        load()->func('file');
        $hander = curl_init();
        curl_setopt($hander,CURLOPT_URL,$_url);
        curl_setopt($hander, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($hander,CURLOPT_HEADER,0);
        curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($hander,CURLOPT_TIMEOUT,60);
        $_data = curl_exec($hander);
        curl_close($hander);
        $_filename = 'kuyuan_red/'.$this->_uniacid.'/member_header/'.'member_header'.$_uid.'.jpg';
        file_write($_filename,$_data);
        return tomedia($_filename);
    }
}